<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索框&添加用户按钮开始 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="搜索用户名"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="searchUserList"
            @change="searchUserList"
          >
            <!-- 放大镜图标 -->
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchUserList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 右侧添加用户按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!-- 搜索框结束 -->
      <!-- 表格模块开始 -->
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        height="660"
      >
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column prop="username" label="用户名"> </el-table-column>
        <el-table-column prop="nickname" label="昵称"> </el-table-column>
        <!-- <el-table-column prop="user_pic" label="头像"> </el-table-column> -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditUserForm(scope.row.id)"
              >编辑</el-button
            >
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteUser(scope.row.id)"
              :disabled="scope.row.username == 'admin' ? true : false"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格模块结束 -->
    </el-card>

    <!-- 添加用户弹框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="addUserFormClose"
    >
      <el-form
        ref="addUserRef"
        :model="addUserFormData"
        label-width="80px"
        :rules="addUsersRules"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addUserFormData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addUserFormData.password" show-password></el-input>
        </el-form-item>
        <!-- <el-form-item label="昵称" prop="nickname">
          <el-input v-model="addUserFormData.nickname"></el-input>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑用户弹框 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogVisible" width="30%">
      <el-form
        ref="editUserRef"
        :rules="addUsersRules"
        :model="editUserFormData"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editUserFormData.username" disabled></el-input>
        </el-form-item>
        <!-- <el-form-item label="密码" prop="password">
          <el-input v-model="editUserFormData.password"></el-input>
        </el-form-item> -->
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="editUserFormData.nickname"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表格数据
      tableData: [
        {
          id: 1,
          username: "1",
          nickname: "1",
          //   user_pic: "1",
        },
      ],
      //搜索条件,没做分页
      queryInfo: {
        query: "",
      },
      // 表格载入状态,如果还在加载就是true
      loading: true,
      // 添加用户对话框显示隐藏
      addDialogVisible: false,
      //编辑用户对话框显示隐藏
      editDialogVisible: false,
      // 添加用户的数据
      addUserFormData: {
        username: "admin",
        password: "123456",
      },
      // 编辑用户的数据
      editUserFormData: {
        id: "",
        username: "admin",
        nickname: "xiaoming",
      },
      // 添加用户校验规则
      addUsersRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名长度需要在3-10个字符之间！",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 4,
            max: 15,
            message: "用户名密码需要在4-15个字符之间！",
            trigger: "blur",
          },
        ],
      },
    };
  },
  // 进入网页就执行获取用户列表函数
  created() {
    this.getUsersList();
  },
  methods: {
    // 获取用户列表函数,带搜索功能
    async getUsersList() {
      let { data: res } = await this.$http.get("users", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.tableData = res.data;
        //加载完成，将加载状态设置为false
        this.loading = false;
      }
    },
    // 点击搜索按钮的事件
    searchUserList() {
      this.getUsersList();
    },
    //删除用户函数
    async deleteUser(id) {
      //这里开始是确认删除对话框
      let confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
      //这里开始是向后端发送删除请求
      if (confirmResult == "confirm") {
        let { data: res } = await this.$http.delete("users/" + id);
        // 判断状态码
        if (res.meta.status !== 200) {
          this.$message.error(res.meta.msg);
        } else {
          this.$message.success(res.meta.msg);
          this.getUsersList();
        }
      }
    },
    //添加用户函数
    addUser() {
      this.$refs.addUserRef.validate(async (valid) => {
        if (valid) {
          // 向后端发送添加请求
          let { data: res } = await this.$http.post(
            "reguser",
            this.addUserFormData
          );
          // 判断状态码
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg);
          } else {
            this.$message.success(res.meta.msg);
            this.addDialogVisible = false;
            this.getUsersList();
          }
        } else {
          return;
        }
      });
    },
    //展示编辑用户对话框的函数,并通过id将信息填入对话框
    async showEditUserForm(id) {
      this.editDialogVisible = true;
      let { data: res } = await this.$http.get(`users/${id}`);
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.editUserFormData = res.data;
      }
    },
    // 编辑用户信息函数
    editUser() {
      this.$refs.editUserRef.validate(async (valid) => {
        //向后端发送请求
        if (valid) {
          let { data: res } = await this.$http.put(
            `users/${this.editUserFormData.id}`,
            this.editUserFormData
          );
          //判断状态码
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg);
          } else {
            this.$message.success(res.meta.msg);
            this.editDialogVisible = false;
            this.getUsersList();
          }
        } else {
          return;
        }
      });
    },
    //关闭对话框时 清空添加的数据
    addUserFormClose() {
      this.$refs.addUserRef.resetFields();
    },
  },
};
</script>
<style lang="less" scoped></style>
